<?php



namespace plugins\store\controller;

use app\common\lib\model_operdate\FileOperate;

use cmf\controller\PluginAdminBaseController;

use think\Db;



// use plugins\audit\interfaceLib\AuditShop;



use plugins\store\model\AuditModel;



use plugins\store\interfaceLib\StoreClassify;



/**



 * 



 */



class AuditStoreController extends PluginAdminBaseController



{



    private $AM;



    private  $pag = "10";



    private  $pageNum = "10";



    protected  function _initialize()



    {

        $this->AM = new AuditModel;

    }







    /**



     * 店铺审核列表



     */



    public function index()



    {



        $param = $this->request->param();



        //搜索参数



        $search['status'] = isset($param['search']['status'])? $param['search']['status'] : '';



        $search['type'] = isset($param['search']['type'])? $param['search']['type'] : '';



        $search['title'] = isset($param['search']['title'])? $param['search']['title'] : '';



        



        //搜索条件



        $where = " `store_status` = 1 ";



        if( !empty($param['search']) ) 



        {



            $where = 1; 



            if($param['search']['status'] >= 1)



            {



                $where = " `store_status`=".$search['status'];



            }



        }



        if(!empty($search['title']))



        {



            if($search['type'] == 1 ) $where .= " and `store_name` like '%".$search['title']."%'";



            if($search['type'] == 2 ) $where .= " and `store_id`='".$search['title']."'";



        }







        //分页传参



        $sear = [



            "search[type]" => isset( $search['type'] ) ? $search['type'] : "",



            "search[title]" => isset( $search['title'] ) ? $search['title'] : "",



            "search[status]" => isset( $search['status'] ) ? $search['status'] : "",



            "pagesize" => isset( $param [ 'pagesize' ] )  ? $param [ 'pagesize' ]   : $this->pageNum,



        ];



        $info = db( 'store' )->where($where)->paginates($sear[ 'pagesize' ] , false , [ 'query'=> $sear ]);



        foreach ($info as  $key =>$in) {



            $in['time'] = db('store_audit')->where(['store_id'=>$in['store_id'],'status'=>$in['store_status']])->order('time desc')->find()['time'];



            $info[$key] = $in;



        }



        $info->appends($search);



        $this->assign('page', $info->render());//单独提取分页出来



        $this->assign( 'data' , $info );



        $this->assign( 'search' , $search );



        return $this->fetch( 'audit_store/index' );



    }







    /**



     * 审核



     * 通过 状态为 2



     */



    public function audit()



    {



        $param = $this->request->post();



        $id = $param['id'];



        $SC = new StoreClassify();



        //修改状态

        $data = db( 'store' )->where('store_id',$id)->setField('store_status',2);

        $data = $SC->createStoreAdmin($id);

        if($data == '修改完成'){

            //添加日志

            $info = $this->AM->journal('store_id',$id,2);

            if(!$info) return api_json_echo( false , '审核日志添加失败' , '' , 101 );

            return api_json_echo( true , '审核通过' , '' , 200 );



        }else{



            return api_json_echo( false , '审核失败' , '' ,100);



        }



        



        



    }







    /**



     * 批量通过



     */



    public function batchAudit()



    {



        $param = $this->request->post();



        $id = $param['id'];



        $SC = new StoreClassify();







        foreach( $id as $key => $value )



        {



            //修改状态



            $data = $SC->createStoreAdmin($value);



            // $data = db( 'store' )->where('store_id',$value)->setField('store_status',2);



            if($data == '修改完成'){



                //添加日志



                $info = $this->AM->journal('store_id',$value,2);



                if(!$info) return api_json_echo( false , '审核日志添加失败，批量审核中断' , '' , 101 );   



            }else{



                return api_json_echo( false , '出现失败批量审核中断' , '' ,100);



            }



        }



        return api_json_echo( true , '审核通过' , '' , 200 );



    }















    /**



     * 驳回



     * store_status 状态为 3



     */



    public function reject()



    {



        $param = $this->request->param();



        $id = $param['id'];



        $reason = $param['reason'];



        //修改状态



        $data = db('store')->where( 'store_id' , $id )->setField( 'store_status' , 3 );



        if(!$data){



            return api_json_echo( false , '驳回失败' , '' , 100 );



        }



        //添加日志



        $info = $this->AM->journal( 'store_id' , $id , 3 , $reason );



        if(!$info){



            return api_json_echo( false , '审核日志添加失败' , '' , 101 );



        }



        return api_json_echo( true , '驳回成功' , '' , 200 );







    }











    /**



     * 批量驳回



     * store_status 状态为 3



     */



    public function batchReject()

    {

        $param = $this->request->param();

        $id = $param['id'];



        $reason = $param['reason'];

        foreach( $id as $key => $value)

        {

            //修改状态

            $data = db('store')->where( 'store_id' , $value )->setField( 'store_status' , 3 );

            if(!$data) return api_json_echo( false , '出现失败批量驳回中断' , '' , 100 );

            //添加日志

            $info = $this->AM->journal( 'store_id' , $value , 3 , $reason );

            if(!$info) return api_json_echo( false , '审核日志添加失败，批量驳回中断' , '' , 101 );

        }

        return api_json_echo( true , '驳回成功' , '' , 200 );

    }







    /**



     * 查看日志



     */



    public function logPage()



    {



        $param = $this->request->param();



        $id =$param['id'];



        $data = db( 'store_audit' )->where('store_id',$id)->order('time desc')->select();



        $this->assign( 'data' , $data );



        return $this->fetch( 'audit_store/logpage' );



    }











    /**



     * 查看提交内容详细



     */



    public function detailsPag()



    {







    }











}